package com.wdb007.venus.web.api.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wdb007.venus.svc.facade.constant.RspCodeEnum;
import com.wdb007.venus.svc.facade.exception.BizException;
import com.wdb007.venus.svc.facade.exception.SysException;
import com.wdb007.venus.web.api.Jsonable;
import com.wdb007.venus.web.api.model.BaseResponse;

public class CheckPageParamFilter implements Filter {

	private static final Logger logger = LoggerFactory.getLogger(CheckPageParamFilter.class);
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        String pagesize = httpRequest.getParameter("pagesize");
		String pagno = httpRequest.getParameter("pageno");
		try {
			checkIsNull(pagesize, "pagesize");
			checkIsNull(pagno, "pageno");
			chain.doFilter(httpRequest, httpResponse);
		}catch(Exception e) {
			logger.error(e.getMessage(), e);
			BaseResponse<Jsonable> res = new BaseResponse();
			res.setResult(0);
			if(e instanceof BizException) {
				BizException bizRuntimeException = (BizException) e;
				res.setCode(bizRuntimeException.getErrorCode());
				res.setMessage(bizRuntimeException.getErrorMsg());
			}else if(e instanceof SysException) {
				SysException sysRuntimeException = (SysException) e;
				res.setCode(sysRuntimeException.getErrorCode());
				res.setMessage(sysRuntimeException.getErrorMsg());
			}else {
				res.setCode(1010080099);
				res.setMessage("系统异常，请联系管理员");
			}
			responseOutWithJson(httpResponse, res);
		}
	}

	private void responseOutWithJson(HttpServletResponse httpResponse, Object response) {
		JSONObject jsonObject = (JSONObject) JSON.toJSON(response);
		httpResponse.setCharacterEncoding("UTF-8");
		httpResponse.setContentType("application/json; charset=utf-8");  
		PrintWriter out = null;  
	    try {  
	        out = httpResponse.getWriter();  
	        out.append(jsonObject.toString());  
	        logger.debug(jsonObject.toString());  
	        out.flush();
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    } finally {  
	        if (out != null) {  
	        }  
	    }  
	}
	
	@Override
	public void destroy() {
	}
	public void checkIsNull(Object obj, String key) throws BizException {
		if(ObjectUtils.isEmpty(obj)) {
			throw new BizException(RspCodeEnum.PARAM_CHECK_IS_NULL.setRspMessage(key));
		}
	}
}
